package com.example.his.api.front.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.example.his.api.db.dao.CustomerDao;
import com.example.his.api.db.dao.OrderDao;
import com.example.his.api.db.pojo.CustomerEntiy;
import com.example.his.api.front.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service("FrontCustomerServiceImpl")
@Slf4j
public class CustomerServiceImpl implements CustomerService {
  
  @Resource
  private RedisTemplate redisTemplate;
  
  @Resource
  private CustomerDao customerDao;
  @Autowired
  private OrderDao orderDao;
  
  
  //  模拟发送验证码
  @Override
  //  public boolean sendSmsCode(String tel) {
  public HashMap sendSmsCode(String tel) {
    //    生成随机六位数字
    String code = RandomUtil.randomNumbers(6);
    //    System.out.println(code);
    
    //    单纯没有短信接口，把六位数字返回给前端
    HashMap res = new HashMap <>();
    
    String key = "sms_code_refresh_" + tel;
    //    判断现在是否禁止向某个电话发送短信验证码
    if(redisTemplate.hasKey(key)) {
      res.put("isSend", false);
      return res;
      //      return false;
    }
    
    //    创建禁止重新发送短信验证码的缓存
    redisTemplate.opsForValue().set(key, code);
    redisTemplate.expire(key, 2, TimeUnit.MINUTES);
    
    //    把数字和电话号码缓存到redis中，并设置过期时间
    key = "sms_code_" + tel;
    redisTemplate.opsForValue().set(key, code);
    redisTemplate.expire(key, 5, TimeUnit.MINUTES);
    
    //    TODO 调用短信接口，发送验证码
    //    没有相应的服务
    
    //    模拟程序走完，将验证码返回给前端
    res.put("isSend", true);
    res.put("code", code);
    return res;
    //    return true;
  }
  
  @Override
  @Transactional
  public HashMap login(String tel, String code) {
    HashMap map = new HashMap();
    
    //   核对短信验证码和手机号
    String key = "sms_code_" + tel;
    if(! redisTemplate.hasKey(key)) {
      map.put("result", false);
      map.put("msg", "短信验证码已过期");
      return map;
    }
    
    String cacheCode = redisTemplate.opsForValue().get(key).toString();
    if(! cacheCode.equals(code)) {
      map.put("result", false);
      map.put("msg", "短信验证码不正确");
      return map;
    }
    
    redisTemplate.delete(key);
    key = "sms_code_refresh_" + tel;
    redisTemplate.delete(key);
    
    Integer id = customerDao.searchIdByTel(tel);
    //    判断是不是新用户
    if(id == null) {
      CustomerEntiy customerEntiy = new CustomerEntiy();
      customerEntiy.setTel(tel);
      //  注册新用户
      customerDao.insert(customerEntiy);
      id = customerEntiy.getId();
      System.out.println("注册新用户-id:" + id);
    }
    
    map.put("id", id);
    map.put("result", true);
    map.put("msg", "登录成功");
    
    return map;
  }
  
  //  查询用户基本信息
  @Override
  public HashMap searchSummary(int id) {
    HashMap map = customerDao.searchById(id);
    map.putAll(orderDao.searchFrontStatistic(id));
    return map;
  }
  
  @Override
  public boolean update(Map param) {
    int rows = customerDao.update(param);
    return rows == 1;
  }
}
